package com.liu.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.R;
import com.liu.eduService.entity.EduTeacher;
import com.liu.eduService.entity.vo.TeacherQuery;
import com.liu.eduService.service.EduTeacherService;
import com.sun.org.apache.xalan.internal.lib.ExsltDynamic;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author LSJ
 * @since 2021-12-14
 */
@RestController
@RequestMapping("/eduService/teacher")
@Accessors(chain = true)
@CrossOrigin // 每个controller都得加
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;


    @GetMapping("findAll")
    @ApiOperation(value = "查询所有的讲师")

    public R list(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("lists",list);
//        return eduTeacherService.list(null);
    }

    @ApiOperation(value = "由ID删除讲师")
    @DeleteMapping("{id}")
    public R removeByid(
            @ApiParam(name = "id",value = "讲师id",required = true)
            @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if (b){
            return R.ok();

        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "分页查询教师")
    @GetMapping("{page}/{limit}")
    public R PageListForTeacher(
        @ApiParam(name = "page",value = "页数",required = true)
        @PathVariable  Long page,
        @ApiParam(name = "limit",value = "显示条数",required = true)
        @PathVariable   Long limit){
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        eduTeacherService.page(teacherPage,null);
        long total = teacherPage.getTotal(); // 总条数
        List<EduTeacher> records = teacherPage.getRecords(); //查询数据列表

        return R.ok().data("total",total).data("records",records);

    }

    @ApiOperation(value = "分页多条件查询教师")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R PageConditionListForTeacher(
            @ApiParam(name = "page",value = "页数",required = true)
            @PathVariable  Long page,
            @ApiParam(name = "limit",value = "显示条数",required = true)
            @PathVariable   Long limit,
            @ApiParam(name = "teacherQuery",value = "讲师条件",required = false)
            @RequestBody(required = false)
            TeacherQuery teacherQuery){
        // 获取条件：
        Page<EduTeacher> pageParam = new Page<>(page,limit);


        // 对 teacherQuery 查询条件进行筛查
        eduTeacherService.pageQuery(pageParam,teacherQuery);
        long total = pageParam.getTotal();

        List<EduTeacher> records = pageParam.getRecords();

        return R.ok().data("total",total).data("records",records);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("/saveTeacher")
    public R saveTeacher(
            @ApiParam(name = "teacher" ,value = "讲师")
            @RequestBody(required = true)
            EduTeacher eduTeacher
    ){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation(value = "由id获取讲师")
    @GetMapping("/queryTeacherByid/{id}")
    public R queryTeacherByid(
            @ApiParam(name = "id" ,value = "讲师id")
            @PathVariable    Long id
    ){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);

    }


    //修改之前 需要根据id获取到对象
    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher/{id}")
    public R updateTeacher(
            @ApiParam(name = "teacher" ,value = "讲师")
            @RequestBody(required = true)
                    EduTeacher eduTeacher,
            @ApiParam(name = "id", value = "讲师ID", required = true)
           @PathVariable String id
    ){
        eduTeacher.setId(id);
        boolean save = eduTeacherService.updateById(eduTeacher);
        return save?R.ok():R.error();
    }
}

